<?php

namespace Xin\ThinkPHP\Http;

use Xin\Auth\Contracts\AuthVerifyType;
use Xin\ThinkPHP\Facade\Auth;

trait HasUser
{

    /**
     * @var \Closure
     */
    protected $userResolverCallback;

    /**
     * 设置用户完成器
     *
     * @param \Closure $resolverCallback
     */
    public function setUserResolver(\Closure $resolverCallback)
    {
        $this->userResolverCallback = $resolverCallback;
    }

    /**
     * 获取用户信息
     *
     * @param string $field
     * @param mixed $default
     * @param int $verifyType
     * @return mixed
     */
    public function user($field = null, $default = null, $verifyType = AuthVerifyType::BASE)
    {
        if (is_null($this->userResolverCallback)) {
            $this->userResolverCallback = function ($field = null, $default = null, $verifyType = AuthVerifyType::BASE) {
                return Auth::getUser($field, $default, $verifyType);
            };
        }

        return call_user_func($this->userResolverCallback, $field, $default, $verifyType);
    }

    /**
     * 获取当前用户ID
     *
     * @param int $verifyType
     * @return int
     */
    public function userId($verifyType = 1)
    {
        return $this->user('id', 0, $verifyType);
    }

}
